#include <sdk.h>
#include <wx/docview.h>
#include "recentitemslist.h"

RecentItemsList::RecentItemsList( const wxString &menuName, const wxString &configPath, int menuID, int firstMenuItemID ) :
  m_list( nullptr ),
  m_menuName( menuName ),
  m_configPath( configPath ),
  m_menuID( menuID ),
  m_firstMenuItemID( firstMenuItemID ) {
}

void RecentItemsList::AddToHistory( const wxString& FileName ) {
  wxString filename = FileName;
  m_list->AddFileToHistory( filename );
  wxMenu* recentFiles = GetMenu();
  if( recentFiles ) {
    ClearMenu( recentFiles );
    BuildMenu( recentFiles );
  }
}

wxString RecentItemsList::GetHistoryFile( size_t id ) const {
  return ( m_list && id < m_list->GetCount() ) ? m_list->GetHistoryFile( id ) : wxString( wxEmptyString );
}

void RecentItemsList::AskToRemoveFileFromHistory( size_t id, bool cannot_open ) {
  if( !m_list || id >= m_list->GetCount() ) {
    return;
  }
  wxString question( _T( "从“最近使用的文件”列表中移除异常文件" ) );
  wxString query( wxEmptyString );
  if( cannot_open ) {
    query << _T( "无法打开文件" ) << _T( "\n" ) << question;
  } else {
    query << question;
  }
  InfoWindow::Tip( query, _T( "错误" ), 3500 );
  m_list->RemoveFileFromHistory( id );
  wxMenu* recentFiles = GetMenu();
  if( recentFiles ) {
    ClearMenu( recentFiles );
    BuildMenu( recentFiles );
  }
}

void RecentItemsList::ClearHistory() {  // 清除历史记录
  while( m_list->GetCount() ) {
    m_list->RemoveFileFromHistory( 0 );
  }
  Manager::Get()->GetConfigManager( _T( "app" ) )->DeleteSubPath( m_configPath );
  Initialize();
}

void RecentItemsList::Initialize() {  // 初始化历史文件记录
  TerminateHistory();
  m_list = new wxFileHistory( 16, m_firstMenuItemID );
  wxMenu* recentFiles = GetMenu();
  if( recentFiles ) {
    wxArrayString files = Manager::Get()->GetConfigManager( _T( "app" ) )->ReadArrayString( m_configPath );
    for( int i = ( int )files.GetCount() - 1; i >= 0; --i ) {
      if( wxFileExists( files[i] ) ) {
        m_list->AddFileToHistory( files[i] );
      }
    }
    BuildMenu( recentFiles );
  }
}

void RecentItemsList::TerminateHistory() {  // 写历史文件记录
  if( m_list ) {
    wxArrayString files;
    for( unsigned int i = 0; i < m_list->GetCount(); ++i ) {
      files.Add( m_list->GetHistoryFile( i ) );
    }
    Manager::Get()->GetConfigManager( _T( "app" ) )->Write( m_configPath, files );
    wxMenu* recentFiles = GetMenu();
    if( recentFiles ) {
      if( !Manager::IsAppShuttingDown() ) {
        ClearMenu( recentFiles );
      } else {
        m_list->RemoveMenu( recentFiles );
      }
    }
    delete m_list;
    m_list = nullptr;
  }
}

void RecentItemsList::BuildMenu( wxMenu *menu ) {
  if( m_list->GetCount() > 0 ) {
    for( size_t i = 0; i < m_list->GetCount(); ++i ) {
      const wxString &name = F( _T( "&%lu " ), static_cast<unsigned long>( i + 1 ) ) + m_list->GetHistoryFile( i );
      menu->AppendRadioItem( m_firstMenuItemID + i, name );
    }
  }
}

void RecentItemsList::ClearMenu( wxMenu *menu ) {
  while( menu->GetMenuItemCount() > 1 ) {
    menu->Delete( menu->GetMenuItems()[0] );
  }
}

wxMenu* RecentItemsList::GetMenu() {
  wxMenuBar *mbar = Manager::Get()->GetAppFrame()->GetMenuBar();
  if( !mbar ) {
    return nullptr;
  }
  int pos = mbar->FindMenu( m_menuName );
  if( pos == wxNOT_FOUND ) {
    return nullptr;
  }
  wxMenu *menu = mbar->GetMenu( pos );
  wxMenu *recentFiles;
  menu->FindItem( m_menuID, &recentFiles );
  return recentFiles;
}
